﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace WebSite.Controllers
{
    public class CommonController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        public CommonController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }
        // GET: /<controller>/
        public IActionResult Error()
        {
            return View();
        }
        public async Task<IActionResult> Updata()
        {
            var date = Request;
            var files = Request.Form.Files;
            long size = files.Sum(f => f.Length);
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;
            List<string> imgs = new List<string>();
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    //string fileExt = GetFileExt(formFile.FileName); //文件扩展名，不含“.”
                    long fileSize = formFile.Length; //获得文件大小，以字节为单位
                    string newFileName = System.Guid.NewGuid().ToString() + "." + "png"; //随机生成新的文件名
                    string filepathStr = webRootPath + "\\upload\\" + newFileName;
                    var filePath = "/upload/" + newFileName;
                    using (var stream = new FileStream(filepathStr, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                    imgs.Add(filePath);
                }
            }

            return Ok(new { count = files.Count,data=imgs,size });
        }
        public IActionResult Upload()
        {
            var date = Request;
            var files = Request.Form.Files;
            long size = files.Sum(f => f.Length);
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;
            List<string> imgs = new List<string>();
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    //string fileExt = GetFileExt(formFile.FileName); //文件扩展名，不含“.”
                    long fileSize = formFile.Length; //获得文件大小，以字节为单位
                    string newFileName = System.Guid.NewGuid().ToString() + "." + "png"; //随机生成新的文件名
                    string filepathStr = webRootPath + "\\upload\\" + newFileName;
                    var filePath = "/upload/" + newFileName;
                    using (var stream = new FileStream(filepathStr, FileMode.Create))
                    {
                        formFile.CopyTo(stream);
                    }
                    imgs.Add(filePath);
                }
            }

            return Json(new { count = files.Count, data = imgs, size });
        }
    }
}
